//
//  SlidScrollView.m
//  InduceTool
//
//  Created by 魏延龙 on 2020/8/6.
//  Copyright © 2020 魏延龙. All rights reserved.
//

#import "SlidScrollView.h"
#import "ContentScrollView.h"
#import "BottomScrollView.h"
#import "FSSegmentTitleView.h"

#define MAXOFFSETY 150  // 最大偏移量
#define StatusH [UIApplication sharedApplication].statusBarFrame.size.height
#define NavigationBarH 44

@interface SlidScrollView()<UIScrollViewDelegate, FSSegmentTitleViewDelegate>
{
    BOOL canMove;
}

@property (nonatomic, strong) ContentScrollView * scrollView;

@property (nonatomic, strong) UIView * topView;

@property (nonatomic, strong) BottomScrollView * bottomView;

@property (nonatomic, strong) FSSegmentTitleView *titleView;

@end
@implementation SlidScrollView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupViews];
    }
    return self;
}

-(void)layoutSubviews {
    self.scrollView.frame = CGRectMake(0, StatusH + NavigationBarH, self.bounds.size.width, self.frame.size.height-StatusH-NavigationBarH);
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height + 200);
    self.topView.frame = CGRectMake(0, 0, self.bounds.size.width, 200);
    self.bottomView.frame = CGRectMake(0, 200, self.bounds.size.width, self.scrollView.frame.size.height);
    self.titleView.frame = CGRectMake(0, MAXOFFSETY, CGRectGetWidth(self.bounds), 50);
}

-(void)setupViews {
    self.backgroundColor = [UIColor redColor];
    self.scrollView = [[ContentScrollView alloc] init];
    [self addSubview:self.scrollView];
    self.scrollView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.delegate = self;
    self.scrollView.backgroundColor = [UIColor greenColor];
    canMove = YES;  // 最开始的时候是可以滑动的
    
    self.topView = [[UIView alloc] init];
    [self.scrollView addSubview:self.topView];
    self.topView.backgroundColor = [UIColor blueColor];
    
    self.titleView = [[FSSegmentTitleView alloc]initWithFrame:CGRectMake(0, MAXOFFSETY, CGRectGetWidth(self.bounds), 50) titles:@[@"全部",@"服饰穿搭",@"生活百货",@"全部",@"服饰穿搭",@"生活百货",@"全部",@"服饰穿搭",@"生活百货"] delegate:self indicatorType:0];
    self.titleView.indicatorColor = [UIColor blueColor];
    [self.topView addSubview:self.titleView];
    self.titleView.backgroundColor = [UIColor lightGrayColor];
    
    self.bottomView = [[BottomScrollView alloc] init];
    self.bottomView.delegate = self;
    [self.scrollView addSubview:self.bottomView];
    self.bottomView.didSelectedBlock = ^{
    };
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(canMove) name:@"OtherCanMove" object:nil];
    if (@available(iOS 11.0, *)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
}


- (void)canMove{
    canMove = YES;
}

#pragma mark - FSSegmentTitleViewDelegate
-(void)FSSegmentTitleView:(FSSegmentTitleView *)titleView startIndex:(NSInteger)startIndex endIndex:(NSInteger)endIndex {
    [self.bottomView scrollToPage:endIndex];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.bottomView) {
        
    } else {
        CGFloat contentOffsetY = scrollView.contentOffset.y;
        CGFloat maxOffsetY = MAXOFFSETY;
        if (contentOffsetY >= maxOffsetY) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"contentCanMove" object:nil]; // 告诉底部内容视图能进行滑动了
            canMove = NO;   // 自己不能滑动了
        }
        if (canMove == NO) {
            [scrollView setContentOffset:CGPointMake(0, maxOffsetY)];
        }
    }
}

// 滚动停止时，触发该函数
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.bottomView) {
           int page = scrollView.contentOffset.x / self.bounds.size.width;
           self.titleView.selectIndex = page;
       }
}

@end
